package com.shycloud.mido.member.service.impl;

import java.util.List;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shycloud.mido.member.api.entity.SmCoursewareGroupFolder;
import com.shycloud.mido.member.mapper.SmCoursewareGroupFolderMapper;
import com.shycloud.mido.member.service.SmCoursewareGroupFolderService;

import lombok.AllArgsConstructor;

@Service
@AllArgsConstructor
public class SmCoursewareGroupFolderServiceImpl extends ServiceImpl<SmCoursewareGroupFolderMapper, SmCoursewareGroupFolder> implements SmCoursewareGroupFolderService {

	/** 课件分组与文件夹关系表 */
	private SmCoursewareGroupFolderMapper groupFolderMapper;

	/**
	 * 移动文件夹至分组
	 *
	 * @param groupIdFrom
	 * @param folderId
	 * @param groupIdTo
	 * @return java.lang.Boolean
	 * @author nianhua.jiang
	 * @date 2020/11/3 13:21
	 **/
	@Override
	@Transactional(rollbackFor = Exception.class, timeout = 36000)
	public Boolean moveFolderToGroup(Integer groupIdFrom, List<Integer> folderIds, Integer groupIdTo) {

		for (Integer folderId : folderIds) {
			//先删除旧的
			groupFolderMapper.delete(new QueryWrapper<SmCoursewareGroupFolder>().eq(SmCoursewareGroupFolder.COL_GROUP_ID, groupIdFrom).eq(SmCoursewareGroupFolder.COL_FOLDER_ID, folderId));
			//再添加新的
			SmCoursewareGroupFolder groupFolder = new SmCoursewareGroupFolder();
			//文件夹ID
			groupFolder.setFolderId(folderId);
			//目标文件夹ID
			groupFolder.setGroupId(groupIdTo);
			groupFolderMapper.insert(groupFolder);
		}

		return true;
	}

}
